package com.xtayfjpk.zookeeper.test.master_workers.callback.master;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.KeeperException.Code;

import com.xtayfjpk.zookeeper.test.master_workers.Master;
import com.xtayfjpk.zookeeper.test.master_workers.callback.BaseChildrenCallback;

public class WorkersGetChildrenCallback extends BaseChildrenCallback {
	private static final Logger LOGGER = Logger.getLogger(MasterCreateCallback.class);
	private Master master;
	
	public WorkersGetChildrenCallback(Master master) {
		this.master = master;
	}
	

	@Override
	protected void connectionLossCaseAction(int rc, String path, Object ctx, List<String> children) {
		master.monitorWorkers();
	}
	
	@Override
	protected void okCaseAction(int rc, String path, Object ctx, List<String> children) {
		LOGGER.info("Successfull got a list of workers: "
				+ children.size() + " workers");
		//如果有worker失联，则需要对失联worker的任务重新分配
		master.reassignAndSet(children);
	}
	
	@Override
	protected void defaultCaseAction(int rc, String path, Object ctx, List<String> children) {
		LOGGER.error("getChildren failed", KeeperException.create(Code.get(rc), path));
	}

}
